/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package simulador;

import Protocolo.*;
import java.util.Random;

/**
 *
 * @author jccaicedo
 */
public class SimuladorCliente  implements ObservarProtocolo{

    static String[] mensajes = {"The","second","problem","is","that","when","writing","to","a","file,","interrupt()","will","not","interrupt","a","write","in","progress.","This","is","technically","correct","as","the","I/O","is","not","blocking","but","actually","writing.","However","it","means","there","is","no","way","to","interrupt","writing","a","large","block","of","data","to","a","slow","device.","It","would","be","useful","if","Sun","modified","the","library"};
    static String[] usuarios = {"Juan", "Pepe", "Pedro", "Lucia", "Maria", "Diana","Carlos","Miguel","Daniel","Anyela","Katherine","Marcela"};

    public SimuladorCliente(String user) throws InterruptedException{
        ConexionCliente cc = new ConexionCliente("localhost", 4444, user);
        cc.agregarObservador(this);
        cc.start();
        Random rand = new Random();
        long espera = rand.nextInt(5000);
        int numMensajes = rand.nextInt(20);
        System.out.println("Simulador: iniciando cliente con " + numMensajes + " mensajes");
        for(int i = 0; i < numMensajes; i++){
            Thread.sleep(espera);
            int m = rand.nextInt(mensajes.length);
            cc.enviarMensaje(mensajes[m]);
            espera = rand.nextInt(5000);
            System.out.println(" * Siguiente espera="+espera);
        }
        System.out.println("Simulador: desconectando");
        cc.desconectar();
    }

    public void recibirMensaje(String usuario, String mensaje) {
        System.out.println(usuario + ": " + mensaje);
    }

    public static void main(String[] args) throws InterruptedException{
            Random r = new Random();
            String user = usuarios[r.nextInt(usuarios.length)];
            new SimuladorCliente(user);
    }
}
